<template>
  <a-modal
    :open="open"
    width="580px"
    :title="rowData?.jhdh ? '自填冲正' : '自填冲正'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="确定"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '120px' } }">
      <a-form-item label="医保区划代码" class="w-full">
        <a-input v-model:value="formState.missionCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="就诊凭证编号" class="w-full">
        <div class="flex">
          <a-input v-model:value="formState.missionName" placeholder="请输入" />
          <a-button type="link" @click="readCard">读取医保卡</a-button>
        </div>
      </a-form-item>
      <a-form-item label="人员编号" class="w-full" v-bind="validateInfos.psn_no">
        <a-input v-model:value="formState.psn_no" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="业务编码" class="w-full">
        <a-input v-model:value="formState.interfacename" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="原发送方报文ID" class="w-full">
        <a-input v-model:value="formState.msgid" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
// const printJson = ref('')
// const jsonStr = ref('')
const jsonStr = ref(
  '{"ybkRquese":{"cardtype":"1","carddata":""},"ybkResonse":{"xb":"女","sfzh":"310104195701174561","lxdh":"65537984","txdz":"淮海中路1799号","yzbm":"200233","klbbz":"1","kh":"D06073096","xm":"张美芳","zhbz":"2000000000000000","dnzhye":0.0,"lnzhye":0.0,"mzzfdxjzfljs":300.0,"zyzfxxzfljs":1200.0,"mzzfdde":300.0,"zyzfdde":1200.0,"tczffdxde":610000.0,"qfxsfdxxfylj":24274.97,"jlch":"100806419510","mzylbkdd":"","br_id":"D06073096","xzqh":"","ybsqjmbz":"1"}}'
)
const patientState = ref({})
const formState = ref({})
const rulesState = ref({
  psn_no: [{ required: true, message: '请读取医保卡' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const readCard = async () => {
  // jsonStr.value = window.openExe?.readSFZ(printJson.value) //调用壳
  const res = await post('/nationalMedicalInsurance/nmiCommon/getPatientlnfo', {
    jsonStr: jsonStr.value,
    mdtrtCertNo: '',
    mdtrtCertType: '03'
  })
  if (res.code !== 0) return message.error(res.msg)
  patientState.value = res.data?.baseinfo || {}
  formState.value.psn_no = patientState.value.psn_no
}
// const getJsonData = async () => {
//   const res = await post('/nationalMedicalInsurance/nmiCommon/getRegistrationBillPrint', { businessType })
//   if (res.code !== 0) return message.error(res.msg)
//   printJson.value = res.data
// }
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/outp/oupCharge/doPunchUp', {
    psnNo: formState.value.psn_no,
    omsgid: formState.value.msgid,
    oinfno: formState.value.interfacename
  })
  if (code === 0) {
    message.success('冲正成功')
    onCancel()
    emit('ok')
  } else {
    message.error(msg)
  }
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      // getJsonData()
      formState.value = props.rowData
      if (props.rowData?.id) {
        formState.value.psn_no = props.rowData?.psnNo || ''
      }
    }
  }
)
</script>
